import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.*;
import java.util.List;

public class LibManage {
    private JFrame frame = new JFrame();
    private JTextField textFieldTest = new JTextField();

    private MainLib mainLib;

    private JPanel panel = new JPanel();
    private JButton buttonSreach = new JButton("搜索书籍");
    private JButton buttonInsert = new JButton("书籍入库");
    private JButton buttonErase = new JButton("书籍出库");
    private JButton buttonShow = new JButton("所有书籍");
    private JButton buttonLend = new JButton("借出书籍");
    private JButton buttonRet = new JButton("归还书籍");
    private JButton buttonUp = new JButton("上一页");
    private JButton buttonDrop = new JButton("下一页");
    private JButton buttonSort = new JButton("id升序");
    private JButton buttonPage = new JButton("1/1");
    private JButton buttonTest = new JButton("check me");

    private EventSreach sreach = new EventSreach();
    private EventInsert insert = new EventInsert();
    private EventErase erase = new EventErase();
    private EventLend lend = new EventLend();
    private EventRet ret = new EventRet();
    private EventUp up = new EventUp();
    private EventDrop drop = new EventDrop();
    private EventShow show = new EventShow();
    private EventSort sort = new EventSort();
    private EventPage page = new EventPage();
    private EventTest test = new EventTest();

    LibManage() {
        // 设置上侧文本框
        textFieldTest.setPreferredSize(new Dimension(textFieldTest.getWidth(), 40));
        textFieldTest.setFont(new Font("微软雅黑", Font.BOLD, 16));
        frame.getContentPane().add(BorderLayout.NORTH, textFieldTest);

        // 设置左侧按钮
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        JButton[] arrButton = {buttonSreach, buttonInsert, buttonErase, buttonLend, buttonRet, buttonUp, buttonDrop, buttonSort, buttonShow, buttonPage};
        ActionListener[] arrEvent = {sreach, insert, erase, lend, ret, up, drop, sort, show, page};
        for (int i = 0; i < arrButton.length; i++) {
            arrButton[i].setPreferredSize(new Dimension(100, 38));
            arrButton[i].setMaximumSize(new Dimension(100, 38));
            arrButton[i].setFont(new Font("微软雅黑", Font.BOLD, 12));
            arrButton[i].addActionListener(arrEvent[i]);
            if (i == arrButton.length - 1) {
                panel.add(Box.createGlue());
            }
            panel.add(arrButton[i]);
            arrButton[i].setSize(100, 38);
        }
        frame.getContentPane().add(BorderLayout.WEST, panel);

        // 数据展示列
        String user,passwd;
        while(true){
            user = JOptionPane.showInputDialog(frame, "请输入sql用户名:");
            passwd = JOptionPane.showInputDialog(frame, "请输入sql密码:");
            try{
                mainLib = new MainLib(user,passwd);
                break;
            }catch(Exception e){
                e.printStackTrace();
            }
        }
        mainLib.upSreach(mainLib.sreachString,page);
        mainLib.switchShow(0);
        mainLib.switchSort(mainLib.sortIndex, page);
        frame.getContentPane().add(BorderLayout.CENTER, mainLib.panel);

        // 一些frame属性
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(800, 550);
        frame.setResizable(false);
        frame.setVisible(true);
    }

    class EventSreach implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            mainLib.sreachString = textFieldTest.getText();
            mainLib.upSreach(mainLib.sreachString, page);
            buttonSreach.revalidate();
        }
    }

    class EventInsert implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            String bookName = new String();
            while(bookName == null || bookName.length() == 0) {
                bookName = JOptionPane.showInputDialog(frame, "请输入书名:");
            }
            String authorName = new String();
            while(authorName == null || authorName.length() == 0) {
                authorName = JOptionPane.showInputDialog(frame, "请输入作者:");
            }
            int price;
            while(true) {
                try {
                    price = Integer.parseInt(JOptionPane.showInputDialog(frame, "请输入价格:"));
                    break;
                }catch (Exception ex) {}
            }
            int id;
            try {
                id = Integer.parseInt(JOptionPane.showInputDialog(frame, "请输入id:"));
            }catch (Exception ex){
                id = 0;
            }

            mainLib.addMassage(new Massage(bookName, authorName, price, id,false));
            mainLib.switchShow(mainLib.showIndex);
            mainLib.switchSort(mainLib.sortIndex, page);
            buttonInsert.revalidate();
        }
    }

    class EventErase implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            mainLib.delMassage(mainLib.chooiceIndex);
            mainLib.switchShow(mainLib.showIndex);
            mainLib.switchSort(mainLib.sortIndex, page);
            buttonErase.revalidate();
        }
    }

    class EventLend implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            mainLib.lendMassage(mainLib.chooiceIndex);
            if (mainLib.showIndex == 2) {
                mainLib.switchShow(mainLib.showIndex);
                mainLib.switchSort(mainLib.sortIndex, page);
            }
            mainLib.initRetList();
            buttonRet.revalidate();
        }
    }

    class EventRet implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            mainLib.retMassage(mainLib.chooiceIndex);
            if (mainLib.showIndex == 1) {
                mainLib.switchShow(mainLib.showIndex);
                mainLib.switchSort(mainLib.sortIndex, page);
            }
            mainLib.initRetList();
            buttonRet.revalidate();
        }
    }

    class EventShow implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            mainLib.showIndex++;
            mainLib.showIndex %= 3;
            mainLib.switchShow(mainLib.showIndex);
            switch (mainLib.showIndex) {
                case 0:
                    buttonShow.setText("所有书籍");
                    break;
                case 1:
                    buttonShow.setText("待还书籍");
                    break;
                case 2:
                    buttonShow.setText("库中书籍");
                    break;
            }
            mainLib.switchSort(mainLib.sortIndex, page);
        }
    }

    class EventUp implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            mainLib.index--;
            if (mainLib.index < 0) {
                mainLib.index = 0;
            }
            mainLib.initRetList(page);
            buttonUp.revalidate();
        }
    }

    class EventDrop implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            mainLib.index++;
            int k = mainLib.size / mainLib.N;
            if (mainLib.size % mainLib.N != 0) {
                k++;
            }
            k = Math.max(1, k);
            if (mainLib.index >= k) {
                mainLib.index = k - 1;
            }
            mainLib.initRetList(page);
            buttonDrop.revalidate();
        }
    }

    class EventSort implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            mainLib.index = 0;
            ++mainLib.sortIndex;
            mainLib.sortIndex %= 8;
            mainLib.switchSort(mainLib.sortIndex, page);
            switch (mainLib.sortIndex) {
                case 0:
                    buttonSort.setText("id升序");
                    break;
                case 1:
                    buttonSort.setText("id降序");
                    break;
                case 2:
                    buttonSort.setText("书名升序");
                    break;
                case 3:
                    buttonSort.setText("书名降序");
                    break;
                case 4:
                    buttonSort.setText("作者升序");
                    break;
                case 5:
                    buttonSort.setText("作者降序");
                    break;
                case 6:
                    buttonSort.setText("价格升序");
                    break;
                case 7:
                    buttonSort.setText("价格降序");
                    break;
            }
        }
    }

    class EventPage implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            mainLib.initRetList(this);
            buttonPage.revalidate();
        }

        void chang() {
            int k = mainLib.size / mainLib.N;
            if (mainLib.size % mainLib.N != 0) {
                k++;
            }
            k = Math.max(1, k);
            buttonPage.setText(mainLib.index + 1 + "/" + k);
        }
    }

    class EventTest implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            buttonTest.setText("love you");
        }
    }
}